<?php

namespace app\api\controller;
use think\Db;
use app\common\controller\Api;
use app\api\model\UserShop as UsershopModel;

/**
 * 首页接口
 */
class Goodsshop extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取安装店
     * 列表
     */
    public function getShop()
    {
        $uid = $this->auth->id;
        $limit = $this->request->param('limit',20);
        $page = $this->request->param('page',1);
        $like = $this->request->param('like','');
        $distance = $this->request->param('distance',''); // 距离
        $category = $this->request->param('category',''); //分类
        $userlat = $this->request->param('lat',''); // 纬度
        $userlng = $this->request->param('lng',''); //	经度

        $UsershopModel = new UsershopModel();
        $shopList = $UsershopModel->Shoplist($uid, $userlat, $userlng, $like, $limit, $page, $category, $distance);
        if ($shopList)
        {
            foreach ($shopList as $key => $val)
            {
                $shopList[$key]['shop_images'] = explode(',',$val['shop_images']);
                $shopList[$key]['logimage']    = empty($val['logimage']) ? $shopList[$key]['shop_images'][0] : $val['logimage'];

            }
            $this->success('获取成功',$shopList,200);
        }else{
            $this->success('获取失败','',200);
        }
    }

    /**
     * 修改营业时间
     */
    public function businesstime()
    {
        $uid = $this->auth->id;
        $start_time = $this->request->post('start_time');
        $end_time   = $this->request->post('end_time');
        $shopInfo = UsershopModel::where('uid',$uid)->find();

        if (!$shopInfo)
        {
            $this->success('暂无店铺', '', 200);
        }

        $data = [];
        if ($start_time)
        {
            $data['start_time'] = $start_time;
        }
        if ($end_time)
        {
            $data['end_time']   = $end_time;
        }

        if (empty($data['start_time']) && empty($data['end_time']))
        {
            $this->success('请选择修改时间', '', 200);
        }

        UsershopModel::where('uid',$uid)->update($data);

        $this->success('发布成功', '', 200);
    }

    /**
     * 添加休息时间
     */
    public function restadd()
    {
        $uid = $this->auth->id;
        $resttime = $this->request->param('resttime');
        $shopInfo = UsershopModel::get(['uid' => $uid]);
        if (!$shopInfo)
            $this->success('暂无店铺信息', '', 400);

        if ($shopInfo['resttime'])
        {
            $date = $shopInfo['resttime'].','.$resttime;
        }else{
            $date = $resttime;
        }

        $shopInfo->resttime = $date;
        $shopInfo->save();

        $this->success('更新成功', $shopInfo->id, 200);
    }

    /**
     * 休息列表
     */
    public function restlist()
    {
        $uid = $this->auth->id;
        $shopInfo = UsershopModel::where('uid', $uid)->field('id,resttime')->find();
        if (!$shopInfo)
            $this->success('暂无店铺信息', '', 400);

        $shopInfo['resttime'] = str_replace("-","/",$shopInfo['resttime']);
        $this->success('查询成功', $shopInfo, 200);
    }

    /**
     * 删除休息时间
     */
    public function restdel()
    {
        $uid = $this->auth->id;
        $resttime = $this->request->param('resttime');
        $shopInfo = UsershopModel::get(['uid' => $uid]);

        if (!$shopInfo)
            $this->success('暂无店铺信息', '', 400);
        $data = explode(',', $shopInfo['resttime']);

        $id = array_search($resttime,$data);
        unset($data[$id]);

        $data = implode(',', $data);
        $shopInfo->resttime = $data;
        $shopInfo->save();
        $this->success('更新成功', $shopInfo->id, 200);




    }
}
